﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace lydoe
{
    public class ImageHandle
    {
        /// <summary>
        /// Ghép ảnh sAvata vào ảnh nền sbg với tọa độ là (ix,iy) và với kích thước là iw và ih
        /// </summary>
        /// <param name="sAvata">avata người dùng</param>
        /// <param name="sName">tên ảnh sẽ xuất</param>
        /// <param name="sbg">ảnh nền</param>
        /// <param name="spathOut">path out</param>
        /// <param name="ix">tọa độ x để chèn ảnh</param>
        /// <param name="iy">tọa độ y để chèn ảnh</param>
        /// <param name="iw">chiều rộng của chèn ảnh</param>
        /// <param name="ih">chiều cao của ảnh chèn</param>
        /// <returns></returns>
        public static string ImageOver(string sAvata, string sName, string sbg, string spathOut,int ix,int iy,int iw,int ih)
        {
            Bitmap bitMapImage = new Bitmap(sbg);
            Graphics graphicImage = Graphics.FromImage(bitMapImage);
            Image image = DownloadImage(sAvata);
            image = ResizeImage(image, iw, ih);
            //Rectangle destRect1 = new Rectangle(195, 168, 100, 100);
            Rectangle destRect1 = new Rectangle(ix, iy, iw, ih);
            graphicImage.DrawImage(image, destRect1, 0, 0, iw, ih, GraphicsUnit.Pixel);
            string sPathOut = spathOut + "/" + sName + ".jpg";
            bitMapImage.Save(sPathOut, ImageFormat.Jpeg);
            graphicImage.Dispose();
            bitMapImage.Dispose();
            return sPathOut;
        }

        public static string ImageOver(string sAvata, string sName, string sbg, string spathOut, int ix, int iy, int iw, int ih,TextDraw oText)
        {
            Bitmap bitMapImage = new Bitmap(sbg);
            Graphics graphicImage = Graphics.FromImage(bitMapImage);
            Image image = DownloadImage(sAvata);
            image = ResizeImage(image, iw, ih);
            //Rectangle destRect1 = new Rectangle(195, 168, 100, 100);
            Rectangle destRect1 = new Rectangle(ix, iy, iw, ih);
            graphicImage.DrawImage(image, destRect1, 0, 0, iw, ih, GraphicsUnit.Pixel);
            //
            //graphicImage.DrawArc(new Pen(Color.MediumOrchid),new Rectangle(oText.ix,oText.iy,oText.iw,oText.ih),0,360);
            //
            graphicImage.DrawString(oText.sText,new Font(oText.sFont, oText.iSize, oText.bBold?FontStyle.Bold:FontStyle.Regular),Brushes.Violet, new Point(oText.ix, oText.iy)); 
            //
            string sPathOut = spathOut + "/" + sName + ".jpg";
            bitMapImage.Save(sPathOut, ImageFormat.Jpeg);
            graphicImage.Dispose();
            bitMapImage.Dispose();
            return sPathOut;
        }

        public static Image DownloadImage(string _URL)
        {
            Image _tmpImage = null;

            try
            {
                // Open a connection
                System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);
                _HttpWebRequest.AllowWriteStreamBuffering = true;
                // You can also specify additional header values like the user agent or the referer: (Optional)
                _HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; _HttpWebRequest.Referer = "http://www.google.com/";
                // set timeout for 20 seconds (Optional)
                _HttpWebRequest.Timeout = 20000;
                // Request response:
                System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();
                // Open data stream:
                System.IO.Stream _WebStream = _WebResponse.GetResponseStream();
                // convert webstream to image
                _tmpImage = Image.FromStream(_WebStream);
                // Cleanup
                _WebResponse.Close();
                _WebResponse.Close();
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
                return null;
            }

            return _tmpImage;
        }



        public static System.Drawing.Image ResizeImage(System.Drawing.Image sourceImage, int width, int height)
        {
            System.Drawing.Image oThumbNail = new Bitmap(sourceImage, width, height);

            Graphics oGraphic = Graphics.FromImage(oThumbNail);

            oGraphic.CompositingQuality = CompositingQuality.HighQuality;

            oGraphic.SmoothingMode = SmoothingMode.HighQuality;

            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

            Rectangle oRectangle = new Rectangle(0, 0, width, height);

            oGraphic.DrawImage(sourceImage, oRectangle);

            return oThumbNail;

        }
    }
}
